﻿//-----------------------------------------------------------------------
// <copyright file="UserInteractionManager.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>17/02/2013</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Common.Model;
using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.Threading;
using ConsoleHoster.Common.View.Popups;
using System;
using System.Windows;
using System.Windows.Media;

namespace ConsoleHoster.Common.Notifications
{
	public class UserInteractionManager : IUserInteractionManager
	{
		private readonly IDispatcher dispatcher;
		private readonly ViewSettings viewSettings;
		private readonly ILogger logger;

		public UserInteractionManager(IDispatcher argDispatcher, ViewSettings argViewSettings, ILogger argLogger)
		{
			ValidationManager.RequireArgumentNotNull(argViewSettings, "argViewSettings");

			this.dispatcher = argDispatcher;
			this.viewSettings = argViewSettings;
			this.logger = new ComponentLogger("argLogger", argLogger);
		}

		public bool Confirm(string argMessage, string argCaption, Func<ImageSource> argIconProvider = null)
		{
			ValueTypeWrapper<MessageBoxResult> tmpResultWrapper = new ValueTypeWrapper<MessageBoxResult>(MessageBoxResult.Cancel);
			this.dispatcher.Invoke(() =>
			{
				tmpResultWrapper.Value = ConfirmationPopup.Show(this.viewSettings.OwnerWindow, argCaption, argMessage, null, null, argIconProvider, this.viewSettings.DefaultWindowStyle);
			});

			return tmpResultWrapper.Value == MessageBoxResult.Yes;
		}

		public bool ConfirmWithDetails(string argMessage, string argCaption, string argOptionalDetails, string argDetailsHeader, Func<ImageSource> argIconProvider = null)
		{
			ValueTypeWrapper<MessageBoxResult> tmpResultWrapper = new ValueTypeWrapper<MessageBoxResult>(MessageBoxResult.Cancel);
			this.dispatcher.Invoke(() =>
			{
				tmpResultWrapper.Value = ConfirmationPopup.Show(this.viewSettings.OwnerWindow, argCaption, argMessage, argOptionalDetails, argDetailsHeader, argIconProvider, this.viewSettings.DefaultWindowStyle);
			});

			return tmpResultWrapper.Value == MessageBoxResult.Yes;
		}

		public void ShowMessage(string argMessage)
		{
			this.dispatcher.Invoke(() =>
				{
					MessageBox.Show(argMessage);
				});
		}
	}
}
